<?php


  class sasmallparcel {
    var $code, $title, $description, $enabled, $num_zones;

// class constructor
    function sasmallparcel() {
    /**************************************************************************
    *                             Constructor                                 *
    **************************************************************************/
      $this->code = 'sasmallparcel';
      $this->title = MODULE_SHIPPING_SASMALLPARCEL_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_SASMALLPARCEL_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_SASMALLPARCEL_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = MODULE_SHIPPING_SASMALLPARCEL_TAX_CLASS;
      $this->enabled = ((MODULE_SHIPPING_SASMALLPARCEL_STATUS == 'True') ? true : false);

      // CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
      $this->num_zones = 2;
    }

// class methods
    function quote($method = '') {
    /**************************************************************************
    *                                Quote                                    *
    * Purpose: Calculate shipping costs and options during checkout           *
    * Input  : order - the order object
    *          shipping_weight - the total weight of the order
    * Output : quote populated with options when all correct,
    *          quote populated with an error message when weight limit exceeded
    *          quote = false when invalid country
    * Created: Charles Oertel 04/2003 (charles@finebushpeople.net)
    **************************************************************************/
      global $order, $shipping_weight;

      $dest_country = $order->delivery['country']['iso_code_2'];
      if ($dest_country == 'ZA') {
          return false;   //'Letter Post is not available for destinations in South Africa.';
      }

      if ($shipping_weight > MODULE_SHIPPING_SASMALLPARCEL_MAX_WEIGHT) { 
        $this->quotes = array('id' => $this->code,
                              'module' => MODULE_SHIPPING_SASMALLPARCEL_TEXT_TITLE,
                              'methods' => array(array('title' => $this->quotes['error'])));
        $this->quotes['error'] = $shipping_weight . ' ' . 
                                 MODULE_SHIPPING_SASMALLPARCEL_TEXT_UNITS . ' ' . 
                                 MODULE_SHIPPING_SASMALLPARCEL_EXCEEDED_WEIGHT . ' ' . 
                                 MODULE_SHIPPING_SASMALLPARCEL_MAX_WEIGHT . ' ' . 
                                 MODULE_SHIPPING_SASMALLPARCEL_TEXT_UNITS;
        return $this->quotes;
      }

      $dest_zone = 0;
      $error = false;

      for ($i=1; $i<=$this->num_zones; $i++) {
        $countries_table = constant('MODULE_SHIPPING_SASMALLPARCEL_COUNTRIES_' . $i);
        $country_zones = split("[,]", $countries_table);
        if (in_array($dest_country, $country_zones)) {
          $dest_zone = $i;
          break;
        }
      }

      if ($dest_zone == 0) {
        $dest_zone = $this->num_zones;    // the zone is the last zone available
      }

      $surfacemail_cost = constant('MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_COST_' . $dest_zone);
      $surfacemail_shipping = ceil($shipping_weight/MODULE_SHIPPING_SASMALLPARCEL_COSTING_UNIT) * $surfacemail_cost;
      $surfacemail_shipping += constant('MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_REGISTRATIONFEE_' . $dest_zone);
      $surfacemail_shipping += constant('MODULE_SHIPPING_SASMALLPARCEL_HANDLING_' . $dest_zone);
      $surfacemail_shipping_method = MODULE_SHIPPING_SASMALLPARCEL_TEXT_SURFACEMAIL_WAY . ' ' . 
                                     $dest_country . ' : ' . $shipping_weight . ' ' . 
                                     MODULE_SHIPPING_SASMALLPARCEL_TEXT_UNITS . ' - ' .
                                     MODULE_SHIPPING_SASMALLPARCEL_TEXT_SURFACEMAIL_DELIVERYTIME;

      $airmail_cost = constant('MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_COST_' . $dest_zone);
      $airmail_shipping = ceil($shipping_weight/MODULE_SHIPPING_SASMALLPARCEL_COSTING_UNIT) * $airmail_cost;
      $airmail_shipping += constant('MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_REGISTRATIONFEE_' . $dest_zone);
      $airmail_shipping += constant('MODULE_SHIPPING_SASMALLPARCEL_HANDLING_' . $dest_zone);
      $airmail_shipping_method = MODULE_SHIPPING_SASMALLPARCEL_TEXT_AIRMAIL_WAY . ' ' . 
                                 $dest_country . ' : ' . $shipping_weight . ' ' . 
                                 MODULE_SHIPPING_SASMALLPARCEL_TEXT_UNITS . ' - ' .
                                 MODULE_SHIPPING_SASMALLPARCEL_TEXT_AIRMAIL_DELIVERYTIME;
      $methods = array();

      if (($method=='surfacemail') or ($method=='')) {
          $methods[] = array( 'id' => 'surfacemail',
                              'title' => $surfacemail_shipping_method,
                              'cost' => $surfacemail_shipping);
      }
      if (($method=='airmail') or ($method=='')) {
          $methods[] = array( 'id' => 'airmail',
                              'title' => $airmail_shipping_method,
                              'cost' => $airmail_shipping);
      }

      $this->quotes = array( 'id' => $this->code,
                             'module' => MODULE_SHIPPING_SASMALLPARCEL_TEXT_TITLE,
                             'methods' => $methods);

      if ($this->tax_class > 0) {
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
      if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_SASMALLPARCEL_INVALID_ZONE;

      return $this->quotes;
    }

    function check() {
    /**************************************************************************
    *                                 Check                                   *
    **************************************************************************/
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SASMALLPARCEL_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
    /**************************************************************************
    *                               Install                                   *
    * Purpose: Creates the correct keys in the configuration table for 
    *          automatic presentation of variables etc.  Pay attention to
    *          the key names and ensure they are returned by the keys() method
    *          below.
    * Output : Table populated with the keys (constants) and set to default
    *          values for South African small parcel letter post rates.
    * Remarks: This is where you define, and initialise all your constants and
    *          administrator-defined variables.
    **************************************************************************/
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable SA Letter Post', 'MODULE_SHIPPING_SASMALLPARCEL_STATUS', 'True', 'Do you want to offer South African Letter Post shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_SHIPPING_SASMALLPARCEL_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_SASMALLPARCEL_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum weight allowed.', 'MODULE_SHIPPING_SASMALLPARCEL_MAX_WEIGHT', '2000', 'Maximum allowed Letter Post parcel weight in grams.', '6', '0', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Weight or portion thereof that cost rates apply to.', 'MODULE_SHIPPING_SASMALLPARCEL_COSTING_UNIT', '100', 'The unit of weight that the costing rate applies to (e.g. R9.20 per X MODULE_SHIPPING_SASMALLPARCEL_TEXT_UNITS).', '6', '0', now())");
      for ($i = 1; $i <= $this->num_zones; $i++) {
        $default_countries = '';
        if ($i == 1) {
          $default_countries = 'AO,BW,BI,KM,CG,ZR,GA,KE,LS,MG,MW,MU,MZ,NA,RE,RW,SC,SZ,TZ,UG,ZM,ZW';
          $airmail_rate_per_unit = '9.20';
          $surfacemail_rate_per_unit = '7.20';
        }
        if ($i == 2) {
          $default_countries = 'All Others'; // this must be the last zone
          $airmail_rate_per_unit = '16.30';
          $surfacemail_rate_per_unit = '8.20';
        }
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Countries', 'MODULE_SHIPPING_SASMALLPARCEL_COUNTRIES_" . $i ."', '" . $default_countries . "', 'Comma separated list of two character ISO country codes that are part of Zone " . $i . ".', '6', '0', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Airmail shipping rates', 'MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_COST_" . $i ."', '" . $airmail_rate_per_unit . "', 'Airmail rate per unit to Zone " . $i . " destinations. Example: 9.20 (rand per 100g).', '6', '0', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Surface mail shipping rates', 'MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_COST_" . $i ."', '" . $surfacemail_rate_per_unit . "', 'Surface mail rate per unit to Zone " . $i . " destinations. Example: 9.20 (rand per 100g).', '6', '0', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Handling Fee', 'MODULE_SHIPPING_SASMALLPARCEL_HANDLING_" . $i ."', '0', 'Handling Fee for this shipping zone', '6', '0', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Airmail registration Fee', 'MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_REGISTRATIONFEE_" . $i ."', '15.00', 'SA Airmail Postal Registration Fee for this shipping zone', '6', '0', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zone " . $i ." Surfacemail registration Fee', 'MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_REGISTRATIONFEE_" . $i ."', '15.00', 'SA Surface mail Postal Registration Fee for this shipping zone', '6', '0', now())");
      }
    }

    function remove() { 
    /**************************************************************************
    *                                Remove                                   *
    * Purpose: Deletes all the configuration keys
    **************************************************************************/
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
    /**************************************************************************
    *                                Remove                                   *
    * Purpose: Returns all the keys used by this module.
    **************************************************************************/
      $keys = array('MODULE_SHIPPING_SASMALLPARCEL_STATUS', 
                    'MODULE_SHIPPING_SASMALLPARCEL_SORT_ORDER', 
                    'MODULE_SHIPPING_SASMALLPARCEL_TAX_CLASS', 
                    'MODULE_SHIPPING_SASMALLPARCEL_COSTING_UNIT', 
                    'MODULE_SHIPPING_SASMALLPARCEL_MAX_WEIGHT');

      for ($i=1; $i<=$this->num_zones; $i++) {
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_COUNTRIES_' . $i;
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_COST_' . $i;
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_COST_' . $i;
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_HANDLING_' . $i;
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_AIRMAIL_REGISTRATIONFEE_' . $i;
        $keys[] = 'MODULE_SHIPPING_SASMALLPARCEL_SURFACEMAIL_REGISTRATIONFEE_' . $i;
      }

      return $keys;
    }
  }
?>
